package com.ktjy.t382.ch02.service.imp;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ktjy.t382.ch02.entity.SysUser;
import com.ktjy.t382.ch02.entity.Transaction_Record;
import com.ktjy.t382.ch02.entity.vo.R;
import com.ktjy.t382.ch02.mapper.TransactionMapper;
import com.ktjy.t382.ch02.mapper.UserService;
import com.ktjy.t382.ch02.service.SysService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.*;

@Service
public class SysServiceImp implements SysService {
    @Resource
    UserService userService;
    @Resource
    TransactionMapper transactionMapper;

    @Override
    public List<SysUser> login(SysUser sysUser) {
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<SysUser>();
        queryWrapper.eq("cardno", sysUser.getCardno());
        queryWrapper.eq("password", sysUser.getPassword());
        return userService.selectList(queryWrapper);
    }

    @Override
    public List<SysUser> getBalance(String cardno) {
        QueryWrapper<SysUser> wrapper = new QueryWrapper<>();
        wrapper.eq("cardno", cardno);
        return userService.selectList(wrapper);
    }

    @Override
    public int update(SysUser sysUser) {
        return userService.updateById(sysUser);
    }

    @Transactional
    @Override
    public Map<String, Object> balance(String name1, String name2, BigDecimal money) {
        Map<String, Object> map = new HashMap<>();
        QueryWrapper<SysUser> wrapper = new QueryWrapper<>();
        wrapper.eq("cardno", name1);
        QueryWrapper<SysUser> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("cardno", name2);
        if (name2 == null || name1 == null) {
            map.put("code", 0);
            map.put("message", "卡号为空");
        }
        SysUser user = userService.selectOne(wrapper);
        SysUser user1 = userService.selectOne(wrapper1);
        if (user == null || user1 == null) {
            map.put("code", 0);
            map.put("message", "转账卡号不存在");
        } else if (user.getBalance().compareTo(money) < 0) {
            map.put("code", 0);
            map.put("message", "余额不足，请重新输入");
        } else {
            SysUser sys1 = new SysUser();
            sys1.setId(user.getId());
            sys1.setBalance(user.getBalance().subtract(money));
            int i1 = userService.updateById(sys1);
            SysUser sys2 = new SysUser();
            sys2.setId(user1.getId());
            sys2.setBalance(user1.getBalance().add(money));
            int i = userService.updateById(sys2);
            if (i == 0 || i1 == 0) {
                map.put("code", 0);
                map.put("message", "转账失败");
            } else {
                Transaction_Record transaction_record = new Transaction_Record();
                transaction_record.setCardno(Integer.parseInt(name1));
                transaction_record.setAmount(money);
                transaction_record.setBalance(user1.getBalance().subtract(money));
                transaction_record.setTransactionType("1");
                int insert = transactionMapper.insert(transaction_record);
                Transaction_Record transaction_record1 = new Transaction_Record();
                transaction_record1.setCardno(Integer.parseInt(name2));
                transaction_record1.setAmount(money);
                transaction_record1.setAmount(user1.getBalance().add(money));
                transaction_record1.setTransactionType("2");
                int insert1 = transactionMapper.insert(transaction_record1);
                map.put("code", 1);
                map.put("message", "转账成功");
            }
        }
        return map;
    }


}
